//
//  MisstionCompleteDetailVC.m
//  KuWoCan
//
//  Created by hwd on 17/3/28.
//  Copyright © 2017年 hwd. All rights reserved.
//

#import "MisstionCompleteDetailVC.h"

#import "NWMissionComRequest.h"
#import "MissionConDetailModel.h"


@interface MisstionCompleteDetailVC () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) NSMutableArray *dataArr;
@property (nonatomic, strong) UIView *noDataView;

@end

@implementation MisstionCompleteDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    
    NWMissionComRequest *request = [[NWMissionComRequest alloc] init];
    [request startRequestGetAllOrderWithUserId:self.userId ?: [Singleton shareInstance].userId];
    [request completion:^(id result, NSString *message, id extension, BOOL succ) {
        if (succ)
        {
            _dataArr = [MissionConDetailModel arrayOfModelsFromDictionaries:result];
            [self setUpUI];
            if (_dataArr.count > 0) {
                [self.noDataView removeFromSuperview];
            } else {
                [self.view addSubview:self.noDataView];
            }
        } else {
            [SVProgressHUD showErrorWithStatus:message];
        }
    }];
    
}

- (void)setUpUI
{
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"明细";
    
    //创建列表
    UITableView *tabView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
    tabView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tabView.estimatedRowHeight = 40.0;
    tabView.rowHeight = UITableViewAutomaticDimension;
    tabView.delegate = self;
    tabView.dataSource = self;
    [self.view addSubview:tabView];
    [tabView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@0);
        make.left.right.equalTo(@0);
        make.bottom.equalTo(@0);
    }];
}

#pragma mark tableViewDelegate,DataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"tempCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        
    }
    
    UILabel *leftLab = [UILabel new];
    leftLab.text = @"20000 元";
    leftLab.font = KwFont(17);
    leftLab.textColor = [UIColor blackColor];
    [cell addSubview:leftLab];
    [leftLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@15);
        make.top.equalTo(@15);
        make.height.equalTo(@25);
        make.bottom.equalTo(@-15);
    }];
    
    UILabel *rightLab = [UILabel new];
    rightLab.text = @"2017-3-28 2:06";
    rightLab.font = KwFont(17);
    rightLab.textColor = [UIColor blackColor];
    [cell addSubview:rightLab];
    [rightLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@-15);
        make.top.equalTo(@15);
        make.height.equalTo(@25);
        make.bottom.equalTo(@-15);
    }];
    
    UIView *lineView = [UIView new];
    lineView.backgroundColor = [UIColor grayColor];
    [cell addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.height.equalTo(@1.5);
        make.bottom.equalTo(@2);
    }];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    MissionConDetailModel *model = _dataArr[indexPath.row];
    leftLab.text = [NSString stringWithFormat:@"%@元",model.totalPrice];
    rightLab.text = [NSString stringWithFormat:@"%@",model.createDate];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (UIView *)noDataView {
    if (!_noDataView) {
        _noDataView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENW, SCREENH - 64)];
        _noDataView.backgroundColor = [UIColor whiteColor];
        UIImageView *imageV = [[UIImageView alloc] initWithImage:kImg(@"nodata")];
        [_noDataView addSubview:imageV];
        [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.and.centerY.equalTo(_noDataView);
            make.size.mas_equalTo(CGSizeMake(149 * screenW_6, 122 * screenW_6));
        }];
        UILabel *label = [[UILabel alloc] init];
        label.text = @"木有数据哟～";
        [_noDataView addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_noDataView);
            make.top.equalTo(imageV.mas_bottom).offset(10 * screenW_6);
        }];
    }
    return _noDataView;
}

@end
